#pragma once

#include <GL/glew.h>

#include <IL/il.h>
#include <IL/ilut.h>

#include "float3.h"
#include "float2.h"
#include "float4x4.h"

using namespace chag; 

class Floor
{
private: 
	GLuint positionBuffer; 
	GLuint texcoordBuffer; 
	GLuint indexBuffer; 
	GLuint vertexArrayObject;
	GLuint m_texture; 
	GLuint normalBuffer; 

public:

	//Konstruktor
	Floor()
	{
		const float positions[] = {
			-330.0f,   -10.0f, 330.0f,		// v0
			-330.0f,   -10.0f, -330.0f,		// v1
			330.0f,   -10.0f, -330.0f,		// v2
			330.0f,   -10.0f, 330.0f,		// v3
		};

		const float normals[] = {
			0.0f,   1.0f, 0.0f,		// v0
			0.0f,   1.0f, 0.0f,		// v1
			0.0f,   1.0f, 0.0f,			// v2
			0.0f,   1.0f, 0.0f,			// v3
		};

		float texcoords[] = {
			0.0f, 0.0f, // (u,v) for v0
			0.0f, 50.0f, // (u,v) for v1
			30.0f, 50.0, // (u,v) for v2
			30.0f, 0.0f, // (u,v) for v3
		};

		const int indices[] = {
			0,1,3, // Triangle 1
			1,2,3,  // Triangle 2
		};

		// Create the buffer objects
		glGenBuffers( 1, &positionBuffer );															
		glBindBuffer( GL_ARRAY_BUFFER, positionBuffer );											
		glBufferData( GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW );			

		glGenBuffers( 1, &normalBuffer );
		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, normalBuffer );										
		glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW );			

		glGenBuffers( 1, &indexBuffer );
		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer );										
		glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW );	

		glGenBuffers( 1, &texcoordBuffer );
		glBindBuffer( GL_ARRAY_BUFFER, texcoordBuffer );
		glBufferData( GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW );

		// Create the vertex array object
		glGenVertexArrays(1, &vertexArrayObject);
		glBindVertexArray(vertexArrayObject);

		glBindBuffer( GL_ARRAY_BUFFER, positionBuffer );	
		glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/ );	
		glBindBuffer( GL_ARRAY_BUFFER, texcoordBuffer );
		glVertexAttribPointer( 1, 2, GL_FLOAT, false, 0, 0 );
		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer );
		glVertexAttribPointer( 2, 2, GL_FLOAT, false, 0, 0 );
		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, normalBuffer );
		
		glEnableVertexAttribArray(0);
		glEnableVertexAttribArray(1);
		glEnableVertexAttribArray(2);

		//************************************
		//			Load Texture
		//************************************

		m_texture = ilutGLLoadImage("floor.jpg");		
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, m_texture);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

		glGenerateMipmap(GL_TEXTURE_2D);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 

	}

	~Floor()
	{
		glDeleteBuffers(1, &positionBuffer);
		glDeleteBuffers(1, &texcoordBuffer);
		glDeleteBuffers(1, &indexBuffer);
		glDeleteVertexArrays(1, &vertexArrayObject);
		glDeleteTextures(1, &m_texture); 
	}

	// Render box
	void draw(int texloc)
	{

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, m_texture);

		glUniform1i(texloc, 0);

		glBindVertexArray(vertexArrayObject);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
	}

};